package com.winter.service;

import com.google.common.base.Strings;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winter.mapper.FinBondMapper;
import com.winter.mapper.FinCommentInfoMapper;
import com.winter.mapper.FinCpaperMapper;
import com.winter.mapper.FinDepositReceiptMapper;
import com.winter.mapper.FinManageMapper;
import com.winter.mapper.FinOnlineMapper;
import com.winter.mapper.FinUnderlineMapper;
import com.winter.mapper.UserInfoMapper;
import com.winter.model.FinCommentInfo;
import com.winter.model.UserInfo;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 评论
 */
@Service
public class FinCommentInfoService {

    @Autowired
    public FinCommentInfoMapper finCommentInfoMapper;

    /**
     * 线上
     */
    @Autowired
    private FinOnlineMapper finOnlineMapper;
    /**
     * 线下
     */
    @Autowired
    private FinUnderlineMapper finUnderlineMapper;

    /**
     * 理财
     */
    @Autowired
    private FinManageMapper finManageMapper;

    /**
     * 票据
     */
    @Autowired
    private FinCpaperMapper finCpaperMapper;

    /**
     * 债券
     */
    @Autowired
    private FinBondMapper finBondMapper;

    /**
     * 存单
     */
    @Autowired
    private FinDepositReceiptMapper finDepositReceiptMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 查看评论列表
     */
    public Result commentList(Long id, Integer relaType, Integer relaChildType, Integer pageSize, Integer pageNumber) {

        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        PageHelper.startPage(pageNumber, pageSize, false);

        List<FinCommentInfo> finCommentInfo = finCommentInfoMapper.commentList_one(id, relaType, relaChildType);
        if (finCommentInfo.size() > 0) {
            finCommentInfo.forEach(finCommentInfo1 -> {
                String createUser = finCommentInfo1.getCreateUser();
                UserInfo userInfo = userInfoMapper.selectByUserNo(createUser);
                if (userInfo != null) {
                    finCommentInfo1.setUserName(userInfo.getUserName());
                    finCommentInfo1.setUserHead(userInfo.getCustImgUrl());
                }
                List<FinCommentInfo> finCommentInfos = childOne(finCommentInfo1.getId());
                finCommentInfo1.setFinCommentInfo(finCommentInfos);
            });
        }
        PageInfo<FinCommentInfo> pageInfo = new PageInfo<>(finCommentInfo);
        return Result.success(pageInfo);
    }


    // 查看下一级
    public List<FinCommentInfo> childOne(Long superId) {
        List<FinCommentInfo> finCommentInfo = finCommentInfoMapper.childOne(superId);
        if (finCommentInfo.size() > 0) {
            finCommentInfo.forEach(f -> {
                String createUser = f.getCreateUser();
                UserInfo userInfo = userInfoMapper.selectByUserNo(createUser);
                if (userInfo != null) {
                    f.setUserName(userInfo.getUserName());
                    f.setUserHead(userInfo.getCustImgUrl());
                }
                List<FinCommentInfo> finCommentInfos = childTwo(f.getId());
                f.setFinCommentInfo(finCommentInfos);
            });
        }
        return finCommentInfo;
    }

    // 查看下下一级
    public List<FinCommentInfo> childTwo(Long superId) {
        List<FinCommentInfo> finCommentInfo = finCommentInfoMapper.childOne(superId);
        if (finCommentInfo.size() > 0) {
            finCommentInfo.forEach(f -> {
                List<FinCommentInfo> finCommentInfos = childOne(f.getId());
                f.setFinCommentInfo(finCommentInfos);
                String createUser = f.getCreateUser();
                UserInfo userInfo = userInfoMapper.selectByUserNo(createUser);
                if (userInfo != null) {
                    f.setUserName(userInfo.getUserName());
                    f.setUserHead(userInfo.getCustImgUrl());
                }
            });
        }
        return finCommentInfo;
    }


    /**
     * 新增评论
     */
    public Result insertComment(FinCommentInfo finCommentInfo) {
        if (Strings.isNullOrEmpty(finCommentInfo.getCreateUser())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (finCommentInfo != null) {
            finCommentInfo.setIsvalid(new BigDecimal("1"));
            // todo  默认审核通过（测试需要展示）
            finCommentInfo.setAuditing("1");
            finCommentInfo.setUpdatetime(new Date());
            finCommentInfo.setCreatetime(new Date());
            int i = finCommentInfoMapper.insertSelective(finCommentInfo);
            // 新增报价平台的评论统计数
            if (finCommentInfo.getCommentType()
                              .equals("0")) {
                //   1 //报价平台 "2";//金融资讯 3";//资产平台 "4";//商学院 "5";//大金融圈
                if (finCommentInfo.getRelaType()
                                  .equals("1")) {
                    // 1线上
                    if (finCommentInfo.getRelaChildType()
                                      .equals("1")) {
                        finOnlineMapper.updateCount(Constants.add, Constants.commentType, finCommentInfo.getRelaId());
                    }
                    // 2线下
                    if (finCommentInfo.getRelaChildType()
                                      .equals("2")) {
                        finUnderlineMapper.updateCount(Constants.add, Constants.commentType, finCommentInfo.getRelaId());
                    }
                    // 3理财
                    if (finCommentInfo.getRelaChildType()
                                      .equals("3")) {
                        finManageMapper.updateCount(Constants.add, Constants.commentType, finCommentInfo.getRelaId());
                    }
                    // 4票据
                    if (finCommentInfo.getRelaChildType()
                                      .equals("4")) {
                        finCpaperMapper.updateCount(Constants.add, Constants.commentType, finCommentInfo.getRelaId());
                    }
                    // 6债券
                    if (finCommentInfo.getRelaChildType()
                                      .equals("6")) {
                        finBondMapper.updateCount(Constants.add, Constants.commentType, finCommentInfo.getRelaId());
                    }

                    //todo 还有存单
                    // 存单
                    if (finCommentInfo.getRelaChildType()
                                      .equals("7")) {
                        finDepositReceiptMapper.updateCount(Constants.add, Constants.commentType, finCommentInfo.getRelaId());
                    }
                }
            }
            if (i > 0) {
                return Result.success();
            }
        }
        return Result.failure(ResultCode.PARAM_IS_BLANK);
    }
}
